package com.my.screenadapter.anim.lottie;

import android.animation.ValueAnimator;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.airbnb.lottie.LottieAnimationView;
import com.airbnb.lottie.LottieComposition;
import com.my.screenadapter.R;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

/**
 * Created by 张英杰 on 2017/3/10.
 * github   https://github.com/airbnb/lottie-android
 * 简书     http://www.jianshu.com/p/9a2136ecbc7b
 * csdn小动画  http://blog.csdn.net/zhang58246500/article/details/45248701
 */

public class LottieShowActivity extends AppCompatActivity {

    private LottieAnimationView animation_view_click,animation_view_net;
    private TextView tv_seek;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lottieshow);
        animation_view_click = (LottieAnimationView)findViewById(R.id.animationview2);
        animation_view_net = (LottieAnimationView)findViewById(R.id.animationview3);
        tv_seek = (TextView)findViewById(R.id.tv_seek);
        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startAnima();
            }
        }); findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopAnima();
            }
        });
        animation_view_click.addAnimatorUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                tv_seek.setText(" 动画进度" +(int) (animation.getAnimatedFraction()*100) +"%");
            }
        });
        findViewById(R.id.button3).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                loadUrl("http://www.chenailing.cn/EmptyState.json");
            }
        });
    }

    /*
   * 开始动画
   */
    private  void startAnima(){

        boolean inPlaying = animation_view_click.isAnimating();
        if (!inPlaying) {
            animation_view_click.setProgress(0f);
            animation_view_click.playAnimation();
        }
    }

    /*
  * 停止动画
  */
    private  void stopAnima(){
        boolean inPlaying = animation_view_click.isAnimating();
        if (inPlaying) {
            animation_view_click.cancelAnimation();
        }
    }

    @Override
    protected void onStop() {
        super.onStop();
        animation_view_click.cancelAnimation();
    }

    private OkHttpClient client;
    //开始下载
    private void loadUrl(String url) {
        Request request;
        try {
            request = new Request.Builder()
                    .url(url)
                    .build();
        } catch (IllegalArgumentException e) {
            return;
        }


        if (client == null) {
            client = new OkHttpClient();
        }
        client.newCall(request).enqueue(new Callback() {
            @Override public void onFailure(Call call, IOException e) {

            }

            @Override public void onResponse(Call call, Response response) throws IOException {
                if (!response.isSuccessful()) {
                }

//                try {
//                    JSONObject json = new JSONObject(response.body().string());
//                    LottieComposition.Factory
//                            .fromJson(getResources(), json, new LottieComposition.OnCompositionLoadedListener() {
//                                @Override
//                                public void onCompositionLoaded(LottieComposition composition) {
//                                    setComposition(composition);
//                                }
//                            });
//                } catch (JSONException e) {
//                }
            }
        });
    }

    private  void setComposition(LottieComposition composition){
        animation_view_net.setProgress(0);
        animation_view_net.loop(true);
        animation_view_net.setComposition(composition);
        animation_view_net.playAnimation();
    }
}
